home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / BinaryInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.3 KB  |  239 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Vector;
  7. import symjava.sql.SQLException;
  8.  
  9. public final class BinaryInputStream extends InputStream {
  10.    private final int METHOD_getData;
  11.    private final int METHOD_setData = 1;
  12.    private final int METHOD_rewind = 2;
  13.    private RemoteObject proxy;
  14.    private byte[] data;
  15.    private int length;
  16.    private int offset;
  17.    private boolean bClosed;
  18.    private boolean bReadCalled;
  19.  
  20.    public BinaryInputStream(int id, ClientSession session) throws SQLException {
  21.       this.proxy = new RemoteObject("ProxyStream", id, session);
  22.       this.length = 0;
  23.       this.offset = 0;
  24.       this.bClosed = false;
  25.       this.bReadCalled = false;
  26.       this.proxy.invokeMethod(2);
  27.    }
  28.  
  29.    public BinaryInputStream(byte[] b) throws SQLException {
  30.       this.proxy = null;
  31.       this.data = b;
  32.       this.length = b.length;
  33.       this.offset = 0;
  34.       this.bClosed = false;
  35.       this.bReadCalled = true;
  36.    }
  37.  
  38.    public BinaryInputStream(String s) throws SQLException {
  39.       this.proxy = null;
  40.       this.length = s.length();
  41.       this.data = new byte[s.length()];
  42.       s.getBytes(0, s.length() - 1, this.data, 0);
  43.       this.offset = 0;
  44.       this.bClosed = false;
  45.       this.bReadCalled = true;
  46.    }
  47.  
  48.    public void close() throws IOException {
  49.       this.bClosed = true;
  50.    }
  51.  
  52.    public int read() throws IOException {
  53.       if (this.bClosed) {
  54.          return -1;
  55.       } else {
  56.          this.bReadCalled = true;
  57.          if (this.offset >= this.length) {
  58.             try {
  59.                this.data = this.getData();
  60.                this.length = this.data.length;
  61.                if (this.length == 0) {
  62.                   this.close();
  63.                   return -1;
  64.                }
  65.  
  66.                this.offset = 0;
  67.             } catch (SQLException e) {
  68.                throw new IOException(((Throwable)e).getMessage());
  69.             }
  70.          }
  71.  
  72.          return this.data[this.offset++] & 255;
  73.       }
  74.    }
  75.  
  76.    byte[] getData() throws SQLException {
  77.       byte[] b = new byte[0];
  78.       if (this.proxy == null) {
  79.          return b;
  80.       } else {
  81.          Vector results = this.proxy.invokeMethod(0);
  82.          NetData d = (NetData)results.elementAt(0);
  83.          b = d.getBytes();
  84.          return b;
  85.       }
  86.    }
  87.  
  88.    public int getInt() throws SQLException {
  89.       if (!this.bReadCalled) {
  90.          try {
  91.             this.read();
  92.          } catch (IOException e) {
  93.             throw new SQLException(((Throwable)e).getMessage());
  94.          }
  95.       }
  96.  
  97.       NetData d = new NetData(this.data);
  98.  
  99.       try {
  100.          return d.getInt();
  101.       } catch (EOFException var2) {
  102.          throw new SQLException("Can't convert data to request format");
  103.       }
  104.    }
  105.  
  106.    public short getShort() throws SQLException {
  107.       if (!this.bReadCalled) {
  108.          try {
  109.             this.read();
  110.          } catch (IOException e) {
  111.             throw new SQLException(((Throwable)e).getMessage());
  112.          }
  113.       }
  114.  
  115.       NetData d = new NetData(this.data);
  116.  
  117.       try {
  118.          return d.getShort();
  119.       } catch (EOFException var2) {
  120.          throw new SQLException("Can't convert data to request format");
  121.       }
  122.    }
  123.  
  124.    public boolean getBool() throws SQLException {
  125.       if (!this.bReadCalled) {
  126.          try {
  127.             this.read();
  128.          } catch (IOException e) {
  129.             throw new SQLException(((Throwable)e).getMessage());
  130.          }
  131.       }
  132.  
  133.       NetData d = new NetData(this.data);
  134.  
  135.       try {
  136.          return d.getBool();
  137.       } catch (EOFException var2) {
  138.          throw new SQLException("Can't convert data to request format");
  139.       }
  140.    }
  141.  
  142.    public byte getByte() throws SQLException {
  143.       if (!this.bReadCalled) {
  144.          try {
  145.             this.read();
  146.          } catch (IOException e) {
  147.             throw new SQLException(((Throwable)e).getMessage());
  148.          }
  149.       }
  150.  
  151.       NetData d = new NetData(this.data);
  152.  
  153.       try {
  154.          return d.getByte();
  155.       } catch (EOFException var2) {
  156.          throw new SQLException("Can't convert data to request format");
  157.       }
  158.    }
  159.  
  160.    public long getLong() throws SQLException {
  161.       if (!this.bReadCalled) {
  162.          try {
  163.             this.read();
  164.          } catch (IOException e) {
  165.             throw new SQLException(((Throwable)e).getMessage());
  166.          }
  167.       }
  168.  
  169.       NetData d = new NetData(this.data);
  170.  
  171.       try {
  172.          return d.getLong();
  173.       } catch (EOFException var2) {
  174.          throw new SQLException("Can't convert data to request format");
  175.       }
  176.    }
  177.  
  178.    public float getFloat() throws SQLException {
  179.       if (!this.bReadCalled) {
  180.          try {
  181.             this.read();
  182.          } catch (IOException e) {
  183.             throw new SQLException(((Throwable)e).getMessage());
  184.          }
  185.       }
  186.  
  187.       NetData d = new NetData(this.data);
  188.  
  189.       try {
  190.          return d.getFloat();
  191.       } catch (EOFException var2) {
  192.          throw new SQLException("Can't convert data to request format");
  193.       }
  194.    }
  195.  
  196.    public double getDouble() throws SQLException {
  197.       if (!this.bReadCalled) {
  198.          try {
  199.             this.read();
  200.          } catch (IOException e) {
  201.             throw new SQLException(((Throwable)e).getMessage());
  202.          }
  203.       }
  204.  
  205.       NetData d = new NetData(this.data);
  206.  
  207.       try {
  208.          return d.getDouble();
  209.       } catch (EOFException var2) {
  210.          throw new SQLException("Can't convert data to request format");
  211.       }
  212.    }
  213.  
  214.    public byte[] getBytes() throws SQLException {
  215.       if (!this.bReadCalled) {
  216.          try {
  217.             this.read();
  218.          } catch (IOException e) {
  219.             throw new SQLException(((Throwable)e).getMessage());
  220.          }
  221.       }
  222.  
  223.       NetData d = new NetData(this.data);
  224.       return d.getBytes();
  225.    }
  226.  
  227.    public String getString() throws SQLException {
  228.       if (!this.bReadCalled) {
  229.          try {
  230.             this.read();
  231.          } catch (IOException e) {
  232.             throw new SQLException(((Throwable)e).getMessage());
  233.          }
  234.       }
  235.  
  236.       return new String(this.data, 0, 0, this.data.length);
  237.    }
  238. }
  239.